package com.hmsm.sys.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.hmsm.server.base.BaseAction;
import com.hmsm.server.util.QueryResult;
import com.hmsm.sys.po.LogInfo;
import com.hmsm.sys.po.UserInfo;

public class LogInfoAction extends BaseAction<UserInfo> {
	
	private static final long serialVersionUID = -3688486717634849791L;
	
	private String nowpage = null;// 页码
	private String maxpage = null;// 每页显示条数	
	private int count;
	private int allPages;
	private int page;//当前页
	private int rowsPage;//每页数量
	private QueryResult<LogInfo> result;// 日志集合
	private Date beginTime;
	private Date endTime;
	private String userName;
	private int type;
	private String info;

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public QueryResult<LogInfo> getResult() {
		return result;
	}

	public void setResult(QueryResult<LogInfo> result) {
		this.result = result;
	}

	public String getNowpage() {
		return nowpage;
	}

	public void setNowpage(String nowpage) {
		this.nowpage = nowpage;
	}

	public String getMaxpage() {
		return maxpage;
	}

	public void setMaxpage(String maxpage) {
		this.maxpage = maxpage;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public int getAllPages() {
		return allPages;
	}

	public void setAllPages(int allPages) {
		this.allPages = allPages;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRowsPage() {
		return rowsPage;
	}

	public void setRowsPage(int rowsPage) {
		this.rowsPage = rowsPage;
	}

	public Date getBeginTime() {
		return beginTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setBeginTime(Date beginTime) {
		this.beginTime = beginTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public String getInfo() {
		return info;
	}

	public void setInfo(String info) {
		this.info = info;
	}

	/**
	 * 分页获取日志信息
	 * @method	manage 原名 getLoginInfoList
	 * @author 何杰
	 * @return
	 */
	public String manage() {
		if (page==0) {
			page = 1;// 当前页数
		}
		if (rowsPage==0) {
			rowsPage = 10;// 每页显示数
		}
		if (nowpage != null && !nowpage.trim().equals("")) {
			page = Integer.parseInt(nowpage.trim());
			if (page == 0) {
				page = 1;
			}
		}
		if (maxpage != null && !maxpage.trim().equals("")) {
			rowsPage = Integer.parseInt(maxpage.trim());
		}
		 count = getLogInfoService().getLogInfoCount(beginTime,endTime,userName,1);//获得总记录条数
		 allPages = count%rowsPage == 0?count/rowsPage:count/rowsPage+1;//获得总页数
		result = getLogInfoService().getLogInfoList(page, rowsPage, allPages,beginTime,endTime,null,userName,count);
		List<LogInfo> logList = result.getResultlist();
		getRequest().setAttribute("logList", logList);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		if (beginTime != null) {
			String time1 = df.format(beginTime);			
			getRequest().setAttribute("beginTime", time1);
		}
		if (endTime != null) {			
			String time2 = df.format(endTime);
			getRequest().setAttribute("endTime", time2);
		}
		getRequest().setAttribute("userName", userName);
		return SUCCESS;
	}
	
	/**
	 * 
	 *  @Description:保存系统错误日志
	 *  @MethodName :saveSysLogInfo
	 *  @Author     :zhangshuai
	 *  @CreateDate :2013-8-9 下午09:01:05 
	 *  @return
	 *
	 */
	public void saveSysErrorInfo() {
		try {
			UserInfo userInfo = this.getSessionUser();
			if (userInfo == null) {
				this.writeResponse("0");
				return;
			}
			getLogInfoService().save(userInfo.getPkId(), userInfo.getName(), 0, info);
			this.writeResponse("1");
		} catch (Exception e) {
			e.printStackTrace();
			this.writeResponse("-1");
		}
		
	}
	
}
